<?php

/**
 * This is the Widget for Update Resource.
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package cmswidgets.resource
 *
 */
class SlideUpdateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {

        $model = new Slide;
        $is_new = false;
        $process = true;
        $types_array = ConstantDefine::fileTypes();

        $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        $slide = GxcHelpers::loadDetailModel('Slide', $id);
        if ($slide) {
            $model->name = $slide->name;
            $model->slide_link = $slide->slide_link;
            $model->slide_order = $slide->slide_order;
            $model->slide_show = $slide->slide_show;
            $model->slide_short = $slide->slide_short;
            $model->slide_images = $slide->slide_images;
        }
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'slide-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        $content_resources = Slide::Resources();
        //Work with Resource Binding
        $test = '';
        $resource = array();
        $resource_upload = array();
        foreach ($content_resources as $res) {
            $resource_upload[] = GxcHelpers::getArrayResourceObjectBinding('resource_upload_' . $res['type']);
        }

        $i = 0;
        $count_resource = 0;
        foreach ($content_resources as $cres) {
            $j = 1;
            foreach ($resource_upload[$i] as $res_up) {
                $j++;
                $count_resource++;
            }
            $i++;
        }
        $i = 0;
        $count_resource = 0;
        foreach ($content_resources as $cres) {
            $j = 1;
            foreach ($resource_upload[$i] as $res_up) {
                $resourcenews = Resource::model()->findByPk($res_up['resid']);
                if ($resourcenews) {
                    $test = $resourcenews->resource_path;
                    $test = str_replace("\\", "/", $test);
                    $resourcenews->resource_path = $test;
                    //print_r($resourcenews); 
                    $resourcenews->save();
                } else {
                    $test = '';
                }

                $j++;
                $count_resource++;
            }
            $i++;
        }

        if (isset($_POST['Slide'])) {
            $model->attributes = $_POST['Slide'];
            if ($test == "") {
                $slide->name = $model->name;
                $slide->slide_link = $model->slide_link;
                $slide->slide_short = $model->slide_short;
                $slide->slide_order = $model->slide_order;
                $slide->slide_show = $model->slide_show;
                if ($slide->save()) {
                    user()->setFlash('success', t('cms', 'Update Slide Successfully!'));
                }
            } else {
                $slide->name = $model->name;
                $slide->slide_link = $model->slide_link;
                $slide->slide_short = $model->slide_short;
                $slide->slide_order = $model->slide_order;
                $slide->slide_show = $model->slide_show;
                $slide->slide_images = $test;
                if ($slide->save()) {
                    user()->setFlash('success', t('cms', 'Update Slide Successfully!'));
                }
            }
        }
        $this->render('cmswidgets.views.slide.slide_form_widget', array(
            'model' => $model,
            'is_new' => $is_new,
            'types_array' => $types_array,
            'content_resources' => $content_resources,
            'slide_images' => $slide->slide_images,
            'slide_id' => $id
        ));
    }

}
